import axios from 'axios'
import {UseUserStore} from "@/store/module/useStore.js";

// create an axios instance
console.log(import.meta.env)
const service = axios.create({
    baseURL: import.meta.env.VITE_BASE_API_URL,
    timeout: 60 * 1000, // request timeout
    // transformRequest: [function (data, config) {
    //     if (config.isJson) {
    //         return data
    //     } else if (config['Content-Type'] !== 'multipart/form-data;charse=UTF-8') {
    //         return qs.stringify(data)
    //     } else {
    //         return data
    //     }
    // }]
})

// request interceptor
service.interceptors.request.use(config => {
    const userStore = UseUserStore()
    // Do something before request is sent
    if (config.method == 'get') {
        config['params'] = config.data;
    }
    if (config.contentType) {
        config.headers['Content-Type'] = config.contentType;
    }
    if (userStore.token) {
        config.headers['Authorization'] = userStore.token
    }
    return config
}, error => {
    // Do something with request error
    console.log(error) // for debug
    Promise.reject(error)
})

// respone interceptor
service.interceptors.response.use(
    // response => response,
    /**
     * 下面的注释为通过在response里，自定义code来标示请求状态
     * 当code返回如下情况则说明权限有问题，登出并返回到登录页
     * 如想通过xmlhttprequest来状态码标识 逻辑可写在下面error中
     * 以下代码均为样例，请结合自生需求加以修改，若不需要，则可删除
     */
    response => {
        //判断是否直接返回相应值
        if (response.config.isReturn) return response
        const res = response.data;
        const userStore = UseUserStore()
        // return response.data
        if (res.code !== 200) {
            if (res.code === 1) {
                ElMessage({
                    message: res.message,
                    type: 'error',
                    duration: 5 * 1000
                })
            } else if (res.code === 500208) {

                ElMessageBox.confirm('会话已过期，请重新登录', ' ', {
                    confirmButtonText: '重新登录',
                    type: 'warning'
                }).then(async () => {
                    try {
                        await userStore.loginOut()
                        window.location.reload()
                    } catch (e) {

                    }
                })
            } else if (res.code === 401) {
                // 请自行在引入 ElMessageBox
                // import { ElMessage, ElMessageBox } from 'element-ui'
                ElMessageBox.confirm('会话已过期，请重新登录', ' ', {
                    confirmButtonText: '重新登录',
                    type: 'warning'
                }).then(async() => {
                    try {
                        await userStore.loginOut()
                        useTagsViewStore.delAllViews()
                        window.location.reload()

                    } catch (e) {

                    }
                })
            } else if (res.code === 403) {
                ElMessageBox.confirm(res.message, ' ', {
                    confirmButtonText: '重新登录',
                    type: 'warning'
                }).then(async () => {
                    await userStore.loginOut()
                    useTagsViewStore.delAllViews()
                    window.location.reload()

                })
            } else if (res.code == 5001) {
                ElMessage({
                    message: res.message,
                    type: 'warning',
                    duration: 5 * 1000
                })
            } else {
                ElMessage({
                    message: res.message,
                    type: 'error',
                    duration: 3 * 1000
                })
            }
            return Promise.reject(res)
        } else {
            return response.data
        }
    },
    error => {
        console.log('err' + error) // for debug
        ElMessage({
            message: error.message || '网络异常',
            type: 'error',
            duration: 5 * 1000
        })
        return Promise.reject(error)
    })
export default service
